Plotting

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

%matplotlib inline
  • Matplotlib is the most widely used plotting library in the ecosystem of python. In the above cel we loaded matplotlib and the relevant libraries.

  • The easiest way to use matplotlib is via pyplot, which allows you to plot 1D and 2D data. Here is a simple example:

# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)

# Plot the points using matplotlib
plt.plot(x, y)
[<matplotlib.lines.Line2D at 0x7f945a793810>]
../_images/intro2viz_3_1.png

if we want to customize plots it is better to plot by first defining fig and ax objecs which have manuy methods for customizing figure resolution and plot related aspects respecticely.

fig, ax = plt.subplots()

y_sin = np.sin(x)
y_cos = np.cos(x)

# Plot the points using matplotlib
ax.plot(x, y_sin)
ax.plot(x, y_cos)

# Specify labels
ax.set_xlabel('x axis label')
ax.set_ylabel('y axis label')
ax.set_title('Sine and Cosine')
ax.legend(['Sine', 'Cosine'])

#fig.savefig("myfig.pdf")
<matplotlib.legend.Legend at 0x7f9458699e10>
../_images/intro2viz_5_1.png

Interactive plots

Plotly

  • Plotly is large multi-language interactive graphing library that covers Python/Julia/R.

  • Plotly-dash is a framework for building web dashborads with itneractive plotly graphs.

  • Plotly-express is a high level library for quick visualizations whihc is similiar to seaborn vs matploltib in its philosophy

Check out this cool website built using Dash-Plotly

import plotly.express as px
df = pd.DataFrame({ 'X':    1*np.random.randn(500), 
                    'Y':    5*np.random.randn(500), 
                    'Z':    1+5*np.random.randn(500),
                    'time':  np.arange(500)
                  })

px.density_heatmap(df, x='X', y='Y')
#px.line(df, x='X', y='Y')
#px.scatter(df, x='X', y='Y')
#px.area(df, x='X', y='Y')
#px.histogram(df, x="X")
fig = px.scatter(df, x="X", y="Y", size=20*np.ones(len(df)), 
                 animation_frame="time", animation_group='Y', color='Y',
                 range_x=[-20,20], range_y=[-20,20]
                 )
fig.show()

Holoviews

Stop plotting your data - annotate your data and let it visualize itself

  • There are too many options for visualizing data and it is impossible to settle on one because different libraries have different strengths depending on the nature of data and visualization.

  • One emerging idea in scientific software design is to create library agnostic tools. E.g if you want to plot histogram you can do it either using several different libraries or using a library agnostic tool by specifying the particular library interface for the visualization.

  • Holoviews provides interface for using matplotlib, plotly, bokeh plotting through a single high level interface.

import holoviews as hv
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/tmp/ipykernel_1840/4081495914.py in <module>
----> 1 import holoviews as hv

/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/holoviews/__init__.py in <module>
     10 
     11 from . import util                                       # noqa (API import)
---> 12 from .annotators import annotate                         # noqa (API import)
     13 from .core import archive, config                        # noqa (API import)
     14 from .core.boundingregion import BoundingBox             # noqa (API import)

/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/holoviews/annotators.py in <module>
      8 import param
      9 
---> 10 from panel.pane import PaneBase
     11 from panel.layout import Row, Tabs
     12 from panel.util import param_name

/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/panel/__init__.py in <module>
----> 1 from . import layout # noqa
      2 from . import links # noqa
      3 from . import pane # noqa
      4 from . import param # noqa
      5 from . import pipeline # noqa

/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/panel/layout/__init__.py in <module>
----> 1 from .accordion import Accordion # noqa
      2 from .base import Column, ListLike, ListPanel, Panel, Row, WidgetBox # noqa
      3 from .card import Card # noqa
      4 from .flex import FlexBox # noqa
      5 from .grid import GridBox, GridSpec # noqa

/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/panel/layout/accordion.py in <module>
      3 from bokeh.models import Column as BkColumn, CustomJS
      4 
----> 5 from .base import NamedListPanel
      6 from .card import Card
      7 

/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/panel/layout/base.py in <module>
      9 from bokeh.models import Column as BkColumn, Row as BkRow
     10 
---> 11 from ..io.model import hold
     12 from ..io.state import state
     13 from ..reactive import Reactive

/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/panel/io/__init__.py in <module>
      7 import sys
      8 
----> 9 from ..config import config
     10 
     11 from .callbacks import PeriodicCallback # noqa

/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/panel/config.py in <module>
     19 )
     20 
---> 21 from .io.notebook import load_notebook
     22 from .io.state import state
     23 

/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/panel/io/notebook.py in <module>
     37 from .embed import embed_state
     38 from .model import add_to_doc, diff
---> 39 from .resources import Bundle, Resources, _env, bundle_resources
     40 from .server import _server_url, _origin_url, get_server
     41 from .state import state

/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/panel/io/resources.py in <module>
     22 from bokeh.resources import Resources as BkResources
     23 from bokeh.settings import settings as _settings
---> 24 from jinja2 import Environment, Markup, FileSystemLoader
     25 
     26 from ..util import url_path

ImportError: cannot import name 'Markup' from 'jinja2' (/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/jinja2/__init__.py)
hv.extension('matplotlib')  # try 'plotly' or 'matplotlib'

data = np.random.randn(20)

# multiplying creates overlay, adding crates subplots
hv.Curve(data) * hv.Scatter(data) + hv.Curve(data)
hv.extension('plotly')

Z = np.sin( np.random.randn(40,40) )
hv.Surface(Z, bounds=(-5, -5, 5, 5))

Widgets

Suppose we would like to explore how the variation of parameter \(\lambda\) affects the following function of a standing wave:

\[f(x) = sin (\omega \cdot x +p)\]
  • Make a python-function which creates a plot as a function of a parameter(s) of interest.

  • Add an interactive widget on top to vary the parameter.

from ipywidgets import widgets

# in jupyter notebook may need to add %matplotlib inline to top cell
@widgets.interact(phase=(0,2*np.pi), freq = (0.1,5))  
def wave(phase=0, freq=0.5):          
    
    x  = np.linspace(0,10,1000)
    y  = np.sin(freq*x+phase)
        
    plt.plot(x, y)
../_images/intro2viz_37_1.png

Holoviews and Dynamic Map

In addition to jupyter widgets Holoviews provides alternative way of creating widgets for data exploration.

hv.extension('plotly')

def wave(phase, freq):
    
    x  = np.linspace(0,10,100)
    y  = np.sin(freq*x+phase)
    
    return hv.Curve( (x,y) )

# Create dynamic map
dmap = hv.DynamicMap(wave, kdims=['phase', 'freq'])

# Specify variables and ranges
dmap.redim.range(phase=(0.0,2*np.pi), freq=(0.1, 5))             
def rand_image(idx):
    '''Generate a sequence of 2D images with normally distributed numbers
    time = length of trajectory or num of images
    '''
    
    data = np.random.randn(1000, 50, 50)
    
    return hv.Image( data[idx] )

# create dynamic map
dmap = hv.DynamicMap(rand_image, kdims=['time'])

# Specify variables and ranges
dmap.redim.range(time=(1,10))  

Additional resoruces.

Matplotlib has a huge scientific user base. This means that you can always find a good working template of any kind of visualization which you want to make. With basic understanding of matplotlib and some solid googling skills you can go very far. Here are some additional resources that you may find helpful